package com.voidforce.activiti.config;

import com.voidforce.activiti.listener.MyEventListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.spring.SpringAsyncExecutor;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.AbstractProcessEngineAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;

@Configuration
public class ActivitiConfig extends AbstractProcessEngineAutoConfiguration {

    @Autowired
    private MyEventListener myEventListener;

    @Bean
    public SpringProcessEngineConfiguration springProcessEngineConfiguration(@Qualifier("dataSource") DataSource dataSource,
                                                                             @Qualifier("transactionManager") PlatformTransactionManager transactionManager,
                                                                             SpringAsyncExecutor springAsyncExecutor) throws IOException {
        SpringProcessEngineConfiguration springProcessEngineConfiguration = this.baseSpringProcessEngineConfiguration(dataSource, transactionManager, springAsyncExecutor);
        springProcessEngineConfiguration.setActivityFontName("宋体");
        springProcessEngineConfiguration.setAnnotationFontName("宋体");
        springProcessEngineConfiguration.setLabelFontName("宋体");

        List<ActivitiEventListener> activitiEventListener=new ArrayList<ActivitiEventListener>();
        activitiEventListener.add(myEventListener );//配置全局监听器
        springProcessEngineConfiguration.setEventListeners(activitiEventListener);

        HashMap<String, List<ActivitiEventListener>> map = new HashMap<>();
        map.put("TASK_CREATED",activitiEventListener);
        springProcessEngineConfiguration.setTypedEventListeners(map);
        /*springProcessEngineConfiguration.setMailServerHost("stmp.163.com");
        springProcessEngineConfiguration.setMailServerPort(25);
        springProcessEngineConfiguration.setMailServerUsername("username");
        springProcessEngineConfiguration.setMailServerPassword("password");*/
        return springProcessEngineConfiguration;
    }
}
